package m2;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140"); // wht6.cn
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();

        /*
        参数：
           1.队列名
           2.是否是持久队列
           3.是否是独占队列、排他队列
           4.是否自动删除
           5.其他属性配置
         */
        c.queueDeclare("task-queue",true,false,false,null);

        //发送消息
        while (true) {
            System.out.print("输入消息： ");
            String s = new Scanner(System.in).nextLine();
            /*
            参数：
               1.""，默认的交换机
               2.队列名
               3.消息的其他参数属性配置
               4.消息数据
             */
            c.basicPublish("", "task-queue", MessageProperties.PERSISTENT_TEXT_PLAIN, s.getBytes());
        }
    }
}
